/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.isel.beans;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import pt.isel.medknow.tables.*;
import pt.isel.medknow.tables.api.HibMethods;

/**
 *
 * @author c0rp
 */
@ManagedBean(name = "select")
@SessionScoped
public class SelectBean implements Serializable  {

    public static final String dateFormatAsString = "yyyy-MM-dd:HH:mm";
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatAsString);

    public static final HibMethods hib = new HibMethods();
    private List<SelectItem> selectDoctors = new ArrayList<SelectItem>();
    private List<SelectItem> selectPatient = new ArrayList<SelectItem>();
    private List<SelectItem> selectFilial = new ArrayList<SelectItem>();
    private List<SelectItem> selectRole = new ArrayList<SelectItem>();
    private List<SelectItem> selectUser = new ArrayList<SelectItem>();
    private String selectedDoctor = "";
    private String selectedPatient = "";
    private String selectedFilial = "";
    private String selectedRole = "";
    private String selectedUser = "";
    private boolean doctorAdd = true;
    private boolean patientAdd = true;
    private boolean userAdd = true;
    private boolean appointmentAdd = true;
    private String radioD = "add";
    private String radioP = "add";
    private String radioA = "add";
    private String radioU = "add";
    private String patientName;
    private String patientDateOfBirth;
    private String doctorName;
    private String appointmentDate;
    private String appointmentAstigmatic;
    private String appointmentTearRate;
    private String appointmentPrescription;
    private String appointmentLenses;
    private String userLogin;
    private String userPassword;

    public void setRadioP(String radioP) {
        if ((radioP != null) && (radioP.equals("add"))) {
            patientAdd = true;
        } else {
            patientAdd = false;
        }
        this.radioP = radioP;
    }

    public void setRadioU(String radioU) {
        if ((radioU != null) && (radioU.equals("add"))) {
            userAdd = true;
        } else {
            userAdd = false;
        }
        this.radioU = radioU;
    }

    public void setRadioD(String radioD) {
        if ((radioD != null) && (radioD.equals("add"))) {
            doctorAdd = true;
        } else {
            doctorAdd = false;
        }
        this.radioD = radioD;
    }

    public void setRadioA(String radioA) {
        if ((radioA != null) && (radioA.equals("add"))) {
            appointmentAdd = true;
        } else {
            appointmentAdd = false;
        }
        this.radioA = radioA;
    }

    public SelectBean() {
    }

    //<editor-fold defaultstate="collapsed" desc="submits">
    public void submitAddAppointment() {
        try {
            hib.addVisit(new Integer(selectedPatient),
                    new Integer(selectedDoctor), dateFormat.parse(appointmentDate));
        } catch (ParseException ex) {
            // FIXME couldnt parse
        }
    }

    public void submitDeleteAppointment() {
        try {
            hib.deleteVisit(new Integer(selectedPatient),
                    new Integer(selectedDoctor), dateFormat.parse(appointmentDate));
        } catch (ParseException ex) {
            // FIXME couldnt parse
        }
    }

    public void submitAddPatient() {
        try {
            // FIXME  validate fields
            hib.addPatient(patientName, dateFormat.parse(patientDateOfBirth));
        } catch (ParseException ex) {
            // FIXME: cannot parse the date
        }
    }

    public void submitAddDoctor() {
        hib.addDoctor(doctorName, new Integer(selectedFilial));
    }

    public void submitDeletePatient() {
        hib.deletePatient(new Integer(selectedPatient));
    }

    public void submitDeleteDoctor() {
        hib.deleteDoctor(new Integer(selectedDoctor));
    }

    public void submitUpdateAppointment() {
        // FIXME add validation
        try {
            Visit visit = hib.getVisit(new Integer(selectedDoctor),
                    new Integer(selectedPatient), dateFormat.parse(appointmentDate));
            hib.addVisitInfo(visit.getVisitId(), appointmentAstigmatic,
                    appointmentTearRate, appointmentPrescription, appointmentLenses);
        } catch (ParseException ex) {
            // FIXME: do smth
        }
    }
    
    public void submitAddUser() {
        hib.addUser(userLogin, userPassword, selectedRole);
    }
    
    public void submitDeleteUser() {
        hib.deleteUser(selectedUser);
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="get select stuff">
    public String getSelectedPatient() {
        selectPatient = new ArrayList<SelectItem>();
        // init patients list
        List<Patient> patiens = hib.listPatients();
        for (Patient patient : patiens) {
            selectPatient.add(new SelectItem(patient.getPatientId(), patient.getName()));
        }
        return selectedPatient;
    }

    public List<SelectItem> getSelectDoctors() {
        // init doctors list
        selectDoctors = new ArrayList<SelectItem>();
        List<Doctor> doctors = hib.listDoctors();
        for (Doctor doctor : doctors) {
            selectDoctors.add(new SelectItem(doctor.getDoctorId(), doctor.getName()));
        }

        return selectDoctors;
    }

    public List<SelectItem> getSelectFilial() {
        // init doctors list
        selectFilial = new ArrayList<SelectItem>();
        List<Filial> filials = hib.listFilials();
        for (Filial filial : filials) {
            selectFilial.add(new SelectItem(filial.getFilialId(), filial.getName()));
        }
        return selectFilial;
    }
    
    public List<SelectItem> getSelectRole() {
        selectRole = new ArrayList<SelectItem>();
        List<RefUserRole> roles = hib.listRoles();
        for (RefUserRole role : roles) {
            selectRole.add(new SelectItem(role.getRolename(), role.getRolename()));
        }
        return selectRole;
    }
    
    public List<SelectItem> getSelectUser() {
        selectUser = new ArrayList<SelectItem>();
        List<RefUserAccount> users = hib.listUsers();
        for (RefUserAccount user : users) {
            selectUser.add(new SelectItem(user.getUsername(), user.getUsername()));
        }
        return selectUser;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="IDE generated stuff">
    public String getRadioU() {
        return radioU;
    }

    public boolean isUserAdd() {
        return userAdd;
    }

    public void setUserAdd(boolean userAdd) {
        this.userAdd = userAdd;
    }

    public boolean isPatientAdd() {
        return patientAdd;
    }

    public void setPatientAdd(boolean patientAdd) {
        this.patientAdd = patientAdd;
    }

    public String getRadioD() {
        return radioD;
    }

    public String getRadioP() {
        return radioP;
    }

    public void setDoctorAdd(boolean doctorAdd) {
        this.doctorAdd = doctorAdd;
    }

    public void setSelectedPatient(String selectedPatient) {
        this.selectedPatient = selectedPatient;
    }

    public void setSelectDoctors(List<SelectItem> selectDoctors) {
        this.selectDoctors = selectDoctors;
    }

    public List<SelectItem> getSelectPatient() {
        return selectPatient;
    }

    public void setSelectPatient(List<SelectItem> selectPatient) {
        this.selectPatient = selectPatient;
    }

    public String getSelectedDoctors() {
        return selectedDoctor;
    }

    public void setSelectedDoctors(String selectedDoctor) {
        this.selectedDoctor = selectedDoctor;
    }

    public String getPatientDateOfBirth() {
        return patientDateOfBirth;
    }

    public void setPatientDateOfBirth(String patientDateOfBirth) {
        this.patientDateOfBirth = patientDateOfBirth;
    }

    public String getPatientName() {
        return patientName;
    }

    public void setPatientName(String patientName) {
        this.patientName = patientName;
    }

    public void setSelectFilial(List<SelectItem> selectFilial) {
        this.selectFilial = selectFilial;
    }

    public String getSelectedDoctor() {
        return selectedDoctor;
    }

    public void setSelectedDoctor(String selectedDoctor) {
        this.selectedDoctor = selectedDoctor;
    }

    public String getSelectedFilial() {
        return selectedFilial;
    }

    public void setSelectedFilial(String selectedFilial) {
        this.selectedFilial = selectedFilial;
    }

    public String getDoctorName() {
        return doctorName;
    }

    public void setDoctorName(String doctorName) {
        this.doctorName = doctorName;
    }

    public String getAppointmentDate() {
        return appointmentDate;
    }

    public void setAppointmentDate(String appointmentDate) {
        this.appointmentDate = appointmentDate;
    }

    public String getRadioA() {
        return radioA;
    }

    public boolean isAppointmentAdd() {
        return appointmentAdd;
    }

    public void setAppointmentAdd(boolean appointmentAdd) {
        this.appointmentAdd = appointmentAdd;
    }

    public boolean isDoctorAdd() {
        return doctorAdd;
    }

    public String getAppointmentAstigmatic() {
        return appointmentAstigmatic;
    }

    public void setAppointmentAstigmatic(String appointmentAstigmatic) {
        this.appointmentAstigmatic = appointmentAstigmatic;
    }

    public String getAppointmentLenses() {
        return appointmentLenses;
    }

    public void setAppointmentLenses(String appointmentLenses) {
        this.appointmentLenses = appointmentLenses;
    }

    public String getAppointmentPrescription() {
        return appointmentPrescription;
    }

    public void setAppointmentPrescription(String appointmentPrescription) {
        this.appointmentPrescription = appointmentPrescription;
    }

    public String getAppointmentTearRate() {
        return appointmentTearRate;
    }

    public void setAppointmentTearRate(String appointmentTearRate) {
        this.appointmentTearRate = appointmentTearRate;
    }

    public String getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(String userLogin) {
        this.userLogin = userLogin;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    
    public void setSelectRole(List<SelectItem> selectRole) {
        this.selectRole = selectRole;
    }

    public void setSelectedRole(String selectedRole) {
        this.selectedRole = selectedRole;
    }
    
    public String getSelectedRole() {
        return selectedRole;
    }

    public void setSelectUser(List<SelectItem> selectUser) {
        this.selectUser = selectUser;
    }

    public String getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(String selectedUser) {
        this.selectedUser = selectedUser;
    }
    
    public String getDateFormatAsString() {
        return dateFormatAsString;
    }
    //</editor-fold>
}
